#include "chattester.h"
#include "ui_chattester.h"

#include "config.h"

ChatTester::ChatTester(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatTester)
{
    ui->setupUi(this);

    m_client = NULL;

    connect(ui->cb_addMyself, SIGNAL(currentIndexChanged(QString)),
            this, SLOT(slotSelectMyClient(QString)));

    m_autoLoginTimer = new QTimer;
    m_autoAddFriendTimer = new QTimer;
    m_updateLcdTimer = new QTimer;
    m_autoChatTimer = new QTimer;
    connect(m_autoChatTimer, SIGNAL(timeout()),
            this, SLOT(slotAudoOnlineChat()));;
    connect(m_autoLoginTimer, SIGNAL(timeout()),
            this, SLOT(slotAutoRegisterAndLogin()));
    connect(m_autoAddFriendTimer, SIGNAL(timeout()),
            this, SLOT(slotAutoAddFriend()));
    connect(m_updateLcdTimer, SIGNAL(timeout()),
            this, SLOT(slotUpdateLcdDisplay()));
}

ChatTester::~ChatTester()
{
    delete m_autoLoginTimer;
    delete m_autoAddFriendTimer;
    delete ui;
}

void ChatTester::slotUserOnline(UserInfo info)
{
    if(!m_onlineUserList.isExistUser(info.m_name))
    {
        m_onlineUserList.appendUserInfo(info);
        m_clientMap.insert(info.m_name, m_client);
        ui->cb_addMyself->addItem(info.m_name);
        ui->cb_addUser->addItem(info.m_name);
        ui->lcd_login->display(++Config::m_socketLoginNumber);
        qDebug() << "Sockets: " << Config::m_socketLoginNumber;

    }
}
void ChatTester::slotUserOffline(UserInfo info)
{
    if(m_onlineUserList.isExistUser(info.m_name))
    {
        ui->cb_addMyself->clear();
        ui->cb_addUser->clear();
        m_onlineUserList.removeUserInfo(info.m_name);
        m_clientMap.remove(info.m_name);
        ui->cb_addMyself->addItems(m_onlineUserList.getUserNameList());
        ui->cb_addUser->addItems(m_onlineUserList.getUserNameList());
        ui->lcd_login->display(--Config::m_socketLoginNumber);
        ui->lcd_login->display(Config::m_socketRegisterNumber - Config::m_socketLoginNumber);
    }
}


void ChatTester::on_pb_newConnect_clicked()
{
    m_client = new AutoClient;
    connect(m_client, SIGNAL(signalUserOffline(UserInfo)),
            this, SLOT(slotUserOffline(UserInfo)));
    connect(m_client, SIGNAL(signalUserOnline(UserInfo)),
            this, SLOT(slotUserOnline(UserInfo)));
    m_client->connectToServer(ui->le_ipAddr->text(), ui->le_port->text().toInt());
}


void ChatTester::on_pb_redister_clicked()
{
    m_client->userRegisterAskMsg(ui->le_rName->text(),
                                 ui->le_rPswd->text());
}

void ChatTester::on_pb_login_clicked()
{
    m_client->userLoginAskMsg(ui->le_rName->text(),
                              ui->le_rPswd->text());
}

void ChatTester::on_pb_addFriend_clicked()
{
    QString name = ui->cb_addMyself->currentText();
    m_clientMap[name]->userAddFriendAskMsg(ui->cb_addUser->currentText());
}

void ChatTester::on_pb_onlineChat_clicked()
{
    QString name = ui->cb_addMyself->currentText();
    if(m_clientMap.contains(name))
    {
        m_clientMap[name]->userChatWithFriend(ui->cb_friend->currentText(), ui->le_chatMsg->text());
    }
}

void ChatTester::slotSelectMyClient(const QString &name)
{
    if((name != "选择自己") && (m_clientMap.contains(name)))
    {
        ui->cb_friend->clear();
        ui->cb_friend->addItem("选择好友");
        QStringList friendList = m_clientMap[name]->m_friendList.getUserNameList();
        ui->cb_friend->addItems(friendList);
    }
}

void ChatTester::slotAutoRegisterAndLogin(void)
{
    QString name = Config::m_userBaseName + QString::number(Config::m_socketRegisterNumber++);
    m_client = new AutoClient;
    connect(m_client, SIGNAL(signalUserOffline(UserInfo)),
            this, SLOT(slotUserOffline(UserInfo)));
    connect(m_client, SIGNAL(signalUserOnline(UserInfo)),
            this, SLOT(slotUserOnline(UserInfo)));
    m_client->connectToServer(ui->le_ipAddr->text(), ui->le_port->text().toInt());
    m_client->userRegisterAskMsg(name,"123");
    ui->lcd_register->display(Config::m_socketRegisterNumber);
}

void ChatTester::slotAutoAddFriend(void)
{
    static QString uName;
    static QString fName;
    int count = m_onlineUserList.m_userCount;
    if(count > 5)
    {
        uName = m_onlineUserList.getUserNameList().at(rand() % count);
        fName = m_onlineUserList.getUserNameList().at(rand() % count);
        if(uName != fName)
        {
            m_clientMap[uName]->userAddFriendAskMsg(fName);
        }
    }
}

void ChatTester::on_pb_autoTest_clicked()
{
    if(ui->cbx_autoLogin->isChecked())
    {
        m_autoLoginTimer->start(Config::m_autoRegLoginTime);
    }else
    {
        m_autoLoginTimer->stop();
    }

    if(ui->cbx_autoAddFriend->isChecked())
    {
        m_autoAddFriendTimer->start(Config::m_autoAddFriendTime);
    }else
    {
        m_autoAddFriendTimer->stop();
    }

    if(ui->cbx_autoOnlineChat->isChecked())
    {
        m_autoChatTimer->start();
    }else
    {
        m_autoChatTimer->stop();
    }

    m_updateLcdTimer->start(1000);
}

void ChatTester::slotUpdateLcdDisplay(void)
{
    ui->lcd_recvRate->display(Config::m_rateRecvByte/1000);
    ui->lcd_sendRate->display(Config::m_rateSendByte/1000);
    if(Config::m_packetRecvNumber > 10000)
    {
        ui->lcd_recv->display(Config::m_packetRecvNumber/10000);
        ui->lb_recv->setText("/万");
    }else
    {
        ui->lcd_recv->display(Config::m_packetRecvNumber);
    }

    if(Config::m_packetSendNumber > 10000)
    {
        ui->lcd_send->display(Config::m_packetSendNumber/10000);
        ui->lb_send->setText("/万");
    }else
    {
        ui->lcd_send->display(Config::m_packetSendNumber);
    }

    if((Config::m_packetSendNumber-Config::m_packetRecvNumber) > 10000)
    {
        ui->lcd_lost->display((Config::m_packetSendNumber-Config::m_packetRecvNumber)/10000);
        ui->lb_lost->setText("/万");
    }else
    {
        ui->lcd_lost->display(Config::m_packetSendNumber-Config::m_packetRecvNumber);
    }

    Config::m_rateRecvByte = 0;
    Config::m_rateSendByte = 0;
}

void ChatTester::slotAudoOnlineChat(void)
{
    static QString uName;
    int count = m_onlineUserList.m_userCount;
    if(count > 10)
    {
        uName = m_onlineUserList.getUserNameList().at(rand() % count);
        m_clientMap[uName]->autoChatWithFriend();
    }
}
